//
//  MyCollectionListVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/6.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "MyCollectionListVC.h"
#import "MyCollectionCompanyTableViewCell.h"
#import "MyCollectionJobTableViewCell.h"
#import "MyCollectionPeopleTableViewCell.h"
#import "RefundListModel.h"
#import "CollectionCompanyModel.h"
#import "CompanyMainViewController.h"
#import "WorkmateMainVC.h"
#import "JobDetailVC.h"
#import "UITableView+Empty.h"

@interface MyCollectionListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrData;
@property (copy, nonatomic)  NSString *type;
@end

@implementation MyCollectionListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    if (self.selectType == 0) {
        self.type = @"COLLECTION_TYPE_COMPANY";
    }else if (self.selectType == 1){
        self.type = @"COLLECTION_TYPE_POSITION";
    }else if (self.selectType == 2){
        self.type = @"COLLECTION_TYPE_USER";
    }
    [self loadTabbleView];
    
    [self downRefreshRequest];
    [self upRefreshRequest];
}


/*
 方法描述:
 加载列表
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadTabbleView{
    [self.tableView registerNib:[UINib nibWithNibName:@"MyCollectionCompanyTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyCollectionCompanyTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyCollectionJobTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyCollectionJobTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyCollectionPeopleTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyCollectionPeopleTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}
#pragma mark ------tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrData.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 65;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CollectionCompanyModel *model = self.arrData[indexPath.row];
    if (self.selectType == 0) {
        /**公司*/
        static NSString *identifier = @"MyCollectionCompanyTableViewCell";
        MyCollectionCompanyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        [cell setDataWithModel:model];
        return cell;
    }else if (self.selectType == 1){
        /**职位*/
        static NSString *identifier = @"MyCollectionJobTableViewCell";
        MyCollectionJobTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        [cell setDataWithModel:model];
        return cell;
    }else if (self.selectType == 2){
         /**同事*/
        static NSString *identifier = @"MyCollectionPeopleTableViewCell";
        MyCollectionPeopleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        [cell setDataWithModel:model];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
    
}


- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
//    ALERT_VIEW(@"等待对方开发完毕直接跳转详情");
    CollectionCompanyModel *model = self.arrData[indexPath.row];
    /**对应id在model中已经标记，注意int类型*/
    if (self.selectType == 0) {
        /**公司*/
        CompanyMainViewController *company = [[CompanyMainViewController alloc] init];
        company.companyId = [NSString stringWithFormat:@"%zd", model.companyId];
        [self.navigationController pushViewController:company animated:YES];
    }else if (self.selectType == 1){
       /**职位*/
        JobDetailVC *job = [[JobDetailVC alloc] init];
        job.positionId = [NSString stringWithFormat:@"%zd", model.positionId];
        [self.navigationController pushViewController:job animated:YES];
    }else if (self.selectType == 2){
        /**同事*/
        WorkmateMainVC *workmate = [[WorkmateMainVC alloc] init];
        workmate.userId = [NSString stringWithFormat:@"%zd", model.userId];
        [self.navigationController pushViewController:workmate animated:YES];
    }
}
#pragma mark -
#pragma mark 下拉刷新
- (void)downRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        weakSelf.page = kStartPage;
        [YJMSendRequest postDataWithParameters:@{@"type":self.type,@"pageSize":kNumOfPageSize,@"pageNo":[NSNumber numberWithInteger:weakSelf.page]} url:@"user/getUserCollected" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                self.arrData = [NSMutableArray array];
                NSArray *tempArr = result[@"data"][@"list"];
                for (NSInteger i = 0; i<tempArr.count; i++) {
                    [self.arrData addObject:[CollectionCompanyModel mj_objectWithKeyValues:tempArr[i][@"details"]]];
                }
                
                if (weakSelf.arrData.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                    
                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
                [weakSelf.tableView.mj_header endRefreshing];
                
            }else{
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
                
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            if (self.arrData.count == 0) {
                [weakSelf.tableView noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                self.tableView.tableFooterView = nil;
            } else {
                [weakSelf.tableView haveDataRemoveEmptyUI];
            }
            [weakSelf.tableView reloadData];
        }];
    }];
    MJHEADER;
    
    
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark 上拉加载
- (void)upRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    
    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [YJMSendRequest postDataWithParameters:@{@"type":self.type,@"pageSize":kNumOfPageSize,@"pageNo":[NSNumber numberWithInteger:weakSelf.page]} url:@"user/getUserCollected" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                NSArray *arrTemp = result[@"data"][@"list"];
                for (NSInteger i = 0; i<arrTemp.count; i++) {
                    [self.arrData addObject:[CollectionCompanyModel mj_objectWithKeyValues:arrTemp[i][@"details"]]];
                }
                

//                
                [self.arrData addObjectsFromArray:arrTemp];
                if (arrTemp.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                    
                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
                [weakSelf.tableView.mj_header endRefreshing];
                
            }else{
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
                
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            if (self.arrData.count == 0) {
                [weakSelf.tableView noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                self.tableView.tableFooterView = nil;
            } else {
                [weakSelf.tableView haveDataRemoveEmptyUI];
            }
            [weakSelf.tableView reloadData];
        }];
    }];
    
    MJFOOTER;
    self.tableView.mj_footer = footer;
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

